package com.future.mayi.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.future.baselib.utils.CommonUtils;
import com.future.baselib.utils.ToastUtil;
import com.future.mayi.R;
import com.future.mayi.activity.EdtExpressActivity;
import com.future.mayi.model.ConsignOrderGoodsModel;
import com.future.mayi.utils.HCUtils;
import com.future.mayi.utils.PriceUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RecycleSelectGoodsDialog extends Dialog implements View.OnClickListener {
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.ll_all)
    LinearLayout llAll;

    private Context mContext;
    private LayoutInflater mInflater;
    private String orderId;
    private List<ConsignOrderGoodsModel> list;

    public RecycleSelectGoodsDialog(@NonNull Context context) {
        super(context, R.style.dialog);

        View layout = LayoutInflater.from(context).inflate(R.layout.dialog_recycle_select_goods, null);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(CommonUtils.getScreenSize(context)[0], ViewGroup.LayoutParams.WRAP_CONTENT);
        setContentView(layout, params);
        if (getWindow() != null) getWindow().setGravity(Gravity.BOTTOM);


        ButterKnife.bind(this);
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }


    @OnClick({R.id.iv_close, R.id.tv_enter})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                dismiss();
                break;
            case R.id.tv_enter:
                String goodsIds = null;
                if (list != null && list.size() > 0) {
                    goodsIds = "";
                    for (ConsignOrderGoodsModel model : list) {
                        if (model == null) continue;
                        if (model.isSelect())
                            goodsIds = goodsIds + String.valueOf(model.getOrder_goods_id()) + ",";
                    }
                    if (goodsIds.length() > 1)
                        goodsIds = goodsIds.substring(0, goodsIds.length() - 1);
                }

                if (TextUtils.isEmpty(goodsIds)) {
                    ToastUtil.getInstance().show(mContext, "请选择寄卖商品");
                    return;
                }

                EdtExpressActivity.startActivity(mContext, orderId, true, goodsIds);
                dismiss();
                break;
        }
    }

    public void show(String orderId, List<ConsignOrderGoodsModel> list) {
        this.orderId = orderId;
        this.list = list;
        llAll.removeAllViews();
        if (list == null || list.size() == 0) return;
        for (ConsignOrderGoodsModel model : list) {
            if (model == null) continue;
            View view = mInflater.inflate(R.layout.layout_recycle_goods_item_select, llAll, false);
            ImageView ivSelect = (ImageView) view.findViewById(R.id.iv_select);
            ImageView ivImg = (ImageView) view.findViewById(R.id.iv_img);
            TextView tvGoodsTitle = (TextView) view.findViewById(R.id.tv_title);
            View llQw = view.findViewById(R.id.ll_qw);
            TextView tvQwType = (TextView) view.findViewById(R.id.tv_qw_type);
            TextView tvQwPrice = (TextView) view.findViewById(R.id.tv_qw_price);
            View llPt = view.findViewById(R.id.ll_pt);
            TextView tvPtType = (TextView) view.findViewById(R.id.tv_pt_type);
            TextView tvPtPrice = (TextView) view.findViewById(R.id.tv_pt_price);
            llQw.setVisibility(View.VISIBLE);
            tvQwType.setText("期望价格：¥");
            tvQwPrice.setText(PriceUtils.getPriceText2(model.getExpect_price()));
            if (model.getPlatform_max_price() > 0) {
                llPt.setVisibility(View.VISIBLE);
                tvPtType.setText("平台报价：¥");
                tvPtPrice.setText(PriceUtils.getPriceText2(model.getPlatform_min_price()) + "~" + PriceUtils.getPriceText2(model.getPlatform_max_price()));
            } else llPt.setVisibility(View.GONE);

            tvGoodsTitle.setText(model.getGoods_name());
            HCUtils.loadWebImg(mContext, ivImg, model.getImg_list() == null || model.getImg_list().size() == 0 ? "" : model.getImg_list().get(0));
            view.setTag(model);
            view.setOnClickListener(this);
            llAll.addView(view);
        }
        super.show();

    }

    @Override
    public void onClick(View view) {
        if (view.getTag() == null) return;
        if (!(view.getTag() instanceof ConsignOrderGoodsModel)) return;
        ConsignOrderGoodsModel model = (ConsignOrderGoodsModel) view.getTag();
        ImageView ivSelect = view.findViewById(R.id.iv_select);
        model.setSelect(!model.isSelect());
        ivSelect.setImageResource(model.isSelect() ? R.drawable.tab_selected : R.drawable.tab_normal);
    }
}
